checkLogin()
woZhenShuai()

// 首页渲染函数
async function renderIndex() {
    // 获取数据 -------------------------------------------------------------------
    const res = await axios({
        url: '/dashboard'
    })
    const result = res.data.data
    // 结构数据 ----------------------------
    const { overview, year, salaryData, groupData, provinceData } = result

    // 渲染overview数据
    Object.keys(overview).forEach(k => {
        document.querySelector(`.${k}`).innerHTML = overview[k]
    })

    // 薪资走势图表
    salaryTrends(year)

    // 班级薪资分布图表
    salaryClass(salaryData)

    // 班级每组薪资图表
    groupSalary(groupData)

    // 男女薪资分布
    genderSalary(salaryData)

    // 籍贯分布
    distribution(provinceData)
}

// 渲染
renderIndex()

// 薪资走势渲染函数
function salaryTrends(year) {
    // x轴数据
    const xdata = []
    // y轴数据
    const ydata = []
    year.forEach(item => {
        Object.keys(item).forEach(k => {
            if (k == 'month') {
                xdata.push(item[k])
            } else {
                ydata.push(item[k])
            }
        })
    })
    // 薪资走势渲染图表
    ech({
        title: {
            text: '薪资走势',
            padding: [5, 0, 0, 20]
        },
        xAxis: {
            type: 'category',
            data: xdata
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: ydata,
                type: 'line',
                smooth: true,
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 0, 1,
                        [{ offset: 0, color: '#229bf5' }, { offset: 1, color: '#fff' }]
                    )
                }
            }
        ]
    }, document.querySelector('#line'))
}

// 班级薪资渲染函数
function salaryClass(salaryData) {
    const newSalaryData = salaryData.map(item => {
        return { value: item.b_count + item.g_count, name: item.label }
    })
    // 班级薪资渲染图表
    ech({
        title: {
            text: '班级薪资分布',
            left: 10
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            bottom: '5%',
            left: 'center'
        },
        series: [
            {
                name: '班级薪资分布情况',
                type: 'pie',
                radius: ['46%', '60%'],
                top: -60,
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 20,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: newSalaryData
            }
        ],
        color: ['#ff9f00', '#2f99fe', '#00bfff', '#00d895']
    }, document.querySelector('#salary'))
}

// 每组薪资渲染函数
function groupSalary(groupData) {
    const btns = document.querySelector('#btns')
    // 渲染分组按钮 
    btns.innerHTML = Object.keys(groupData).map((item, index) => {
        return `<button type="button" class="btn btn-xs ${index ? '' : 'btn-blue'}" data-index="${index + 1}">${index + 1}</button>`
    }).join('')
    // 先渲染一次
    ech({
        legend: {},
        tooltip: {},
        dataset: {
            dimensions: ['name', { name: 'hope_salary', displayName: '组员期望薪资' }, { name: 'salary', displayName: '组员实际薪资' }],
            source: groupData[`1`]
        },
        xAxis: { type: 'category' },
        yAxis: {},
        series: [{
            type: 'bar',
            itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: '#00d796' },
                    { offset: 1, color: '#cdf6e9' }
                ])
            }
        },
        {
            type: 'bar',
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#23a5f5' },
                { offset: 1, color: '#d4ebfd' }
            ])
        }]
    }, document.querySelector('#lines'))
    // 绑定分组按钮点击事件 渲染当前页的数据
    btns.addEventListener('click', e => {
        if (e.target.classList.contains('btn')) {
            // 按钮高亮
            document.querySelector('.btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
            // 渲染
            ech({
                dataset: {
                    dimensions: ['name', { name: 'hope_salary', displayName: '组员期望薪资' }, { name: 'salary', displayName: '组员实际薪资' }],
                    source: groupData[`${e.target.dataset.index}`]
                },
            }, document.querySelector('#lines'))
        }
    })
}

// 男女薪资渲染函数
function genderSalary(salaryData) {
    // 男数据
    const manData = salaryData.map(item => {
        return { name: item.label, value: item.g_count }
    })
    // 女数据
    const womanData = salaryData.map(item => {
        return { name: item.label, value: item.b_count }
    })
    // 整体数据
    let datas = [manData, womanData]
    // 渲染
    ech({
        title: [
            {
                text: '男女薪资分布',
                left: 10,
                textStyle: {
                    fontSize: 16
                }
            },
            {
                text: '男马喽',
                left: '44%',
                top: '46%',
                textStyle: {
                    fontSize: 14,
                    fontWeight: 600
                }
            },
            {
                text: '女马喽',
                left: '44%',
                top: '89%',
                textStyle: {
                    fontSize: 14,
                    fontWeight: 600
                }
            }
        ],
        series: datas.map(function (data, idx) {
            let top = idx * 43.3;
            return {
                type: 'pie',
                radius: [40, 60],
                top: top + '%',
                height: '53.33%',
                left: 'center',
                width: 300,
                itemStyle: {
                    borderColor: '#fff',
                    borderWidth: 1
                },
                label: {
                    alignTo: 'edge',
                    formatter: '{name|{b}}\n{time|{c} 人}',
                    minMargin: 5,
                    edgeDistance: 10,
                    lineHeight: 15,
                    rich: {
                        time: {
                            fontSize: 10,
                            color: '#999'
                        }
                    }
                },
                labelLine: {
                    length: 15,
                    length2: 0,
                    maxSurfaceAngle: 80
                },
                data: data
            }
        }),
        color: ['#ff9f00', '#2f99fe', '#00bfff', '#00d895']
    }, document.querySelector('#gender'))
}

// 籍贯分布渲染函数
function distribution(provinceData) {
    // 处理数据
    const dataList = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 },
    ]
    // 处理数据
    dataList.forEach(item => {
        const res = provinceData.find(ele => ele.name.includes(item.name))
        if (res) {
            item.value = res.value
        }
    })

    // 渲染
    ech({
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        tooltip: {
            trigger: 'item',
            formatter: `{b}: {c} 位学员`,
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,0.5)',
            textStyle: {
                color: '#fff',
            },
        },
        visualMap: {
            min: 0,
            max: 6,
            left: 'left',
            bottom: '20',
            text: ['6', '0'],
            inRange: {
                color: ['#ffffff', '#0075F0'],
            },
            show: true,
            left: 40,
        },
        geo: {
            map: 'china',
            roam: false,
            zoom: 1.0,
            label: {
                normal: {
                    show: true,
                    fontSize: '10',
                    color: 'rgba(0,0,0,0.7)',
                },
            },
            itemStyle: {
                normal: {
                    borderColor: 'rgba(0, 0, 0, 0.2)',
                    color: '#e0ffff',
                },
                emphasis: {
                    areaColor: '#34D39A',
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowBlur: 20,
                    borderWidth: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)',
                },
            },
        },
        series: [
            {
                name: '籍贯分布',
                type: 'map',
                geoIndex: 0,
                data: dataList,
            },
        ],
    }, document.querySelector('#map'))
}


// echarts 图表
function ech(option, ele) {
    const myChart = echarts.init(ele)
    option && myChart.setOption(option)
}
